import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:flutter/material.dart';
import 'package:qianyi/controller/homeTabController.dart';
import 'package:qianyi/controller/imageController.dart';
import 'package:qianyi/controller/imageDetailUserController.dart';
import 'package:qianyi/controller/webSockerController.dart';
import 'package:qianyi/route.dart';
import 'package:qianyi/state/token.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:qianyi/utils/throttle.dart';
import 'package:qianyi/wcao/ui/theme.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
// import 'package:video_js/video_js.dart';

import 'controller/VideoPlayerController.dart';
import 'controller/imageDetailVideoController.dart';

// import 'contr9oller/homeTabController.dart';
void main() {
  // VideoJsResults().init();
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  Get.put(TokenController());
  Get.put(HomeTabController());
  Get.put(ImageDetailVideoController());
  Get.put(WebSocketController());
  Get.put(ImageDetailUserController());
  Get.put(ImageAvatarController());
  // Get.put(ImageVideoPlayerController());
  Get.put(MyVideoPlayerController());
  HudUtil.ready();


  runApp(const MyApp());


}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late DateTime lastPopTime;

  @override
  void initState() {
    super.initState();

    initialization();
  }

  void initialization() async {
    /// HACK: 启动页关闭
    await Future.delayed(const Duration(milliseconds: 1500));
    FlutterNativeSplash.remove();
  }

  @override
  Widget build(BuildContext context) {
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 2000)
      ..indicatorType = EasyLoadingIndicatorType.chasingDots
      ..loadingStyle = EasyLoadingStyle.custom

      ..textColor = Colors.transparent
      ..maskColor = Colors.black.withOpacity(0.5)
      ..userInteractions = true
      ..dismissOnTap = true

      ..indicatorColor = Colors.transparent
      ..backgroundColor = Colors.transparent
      ..boxShadow = [] ;
    return
      WillPopScope(
        child: GetMaterialApp(
      defaultTransition: Transition.fadeIn,
      title: '离火app',
      theme: ThemeData(
        appBarTheme: AppBarTheme(
          toolbarHeight: 40,
          systemOverlayStyle: SystemUiOverlayStyle(
              statusBarColor: Colors.transparent,

            statusBarIconBrightness: Brightness.dark,
          ),

          brightness: Brightness.light,
          elevation: 0,
          centerTitle: true,
          color: Colors.white,
          titleTextStyle: TextStyle(
            color: WcaoTheme.base,
            fontSize: WcaoTheme.fsXl,
            fontWeight: FontWeight.w300,
          ),
          iconTheme: IconThemeData(
            color: WcaoTheme.base,
            size: WcaoTheme.fsXl,
          ),
        ),
        primaryColor: WcaoTheme.primary,
        // backgroundColor: Colors.transparent,
        textTheme: TextTheme(
          bodyText1: TextStyle(fontSize: WcaoTheme.fsBase, fontWeight: FontWeight.w300),
          bodyText2: TextStyle(fontSize: WcaoTheme.fsBase, fontWeight: FontWeight.w300),

        ),
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
        // indicatorColor: Color(0xFFB4B7C6),
        /// textButton 点击波纹
        textButtonTheme: TextButtonThemeData(
          style: ButtonStyle(
            overlayColor: MaterialStateProperty.all(WcaoTheme.primary.withOpacity(0.5)),
            textStyle: MaterialStateProperty.all(
              TextStyle(color: WcaoTheme.primary),
            ),
          ),
        ),

        /// 取消slider间距
        sliderTheme: SliderThemeData(
          trackHeight: 12,
          overlayShape: SliderComponentShape.noOverlay,
          activeTrackColor: WcaoTheme.primary,
          inactiveTrackColor: WcaoTheme.primary.withOpacity(.2),
          thumbColor: WcaoTheme.primary,
          valueIndicatorColor: WcaoTheme.primary,
          inactiveTickMarkColor: WcaoTheme.primary,
        ),
      ),
      getPages: getRoutes,
      initialRoute: '/home',
      builder: EasyLoading.init(),
    ),
        onWillPop: () async {
          if(lastPopTime == null || DateTime.now().difference(lastPopTime) > Duration(seconds: 2)){
            lastPopTime = DateTime.now();
            // Fluttertoast.showToast(msg: '请再按一次退出！');
            return false;
          }else{
            lastPopTime = DateTime.now();
            // 退出app
            await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
            return true;
          }
        })
    ;
  }
}